﻿using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Xml;

namespace KeePassLib.Keys
{
    public class SaveCompositeKey
    {
        public static void Save(CompositeKey ck,string fileLocation)
        {
            string dir = Path.GetDirectoryName(fileLocation);
            string name = Path.GetFileNameWithoutExtension(fileLocation) + ".ckf";
            string compKeyFile = Path.Combine(dir, name);


            XmlDocument compositeKeyXML = new XmlDocument();
            XmlNode header = compositeKeyXML.CreateNode(XmlNodeType.XmlDeclaration, "", "");
            compositeKeyXML.AppendChild(header);
            XmlElement compositeKeyEl = compositeKeyXML.CreateElement("CompositeKey");
            compositeKeyXML.AppendChild(compositeKeyEl);  
            foreach (IUserKey uk in ck.UserKeys)
            {
                Type keyType=uk.GetType();
                if (keyType == typeof(KcpPassword))
                {
                    XmlElement passwordEl = compositeKeyXML.CreateElement("Password");
                    compositeKeyEl.AppendChild(passwordEl);
                    passwordEl.InnerText = System.Text.Encoding.ASCII.GetString(((KcpPassword)uk).Password.ReadUtf8()); 
                }
                else if (keyType == typeof(KcpUserAccount))
                {
                    XmlElement userAccEl = compositeKeyXML.CreateElement("UserAccount");
                    compositeKeyEl.AppendChild(userAccEl);
                    //KcpUserAccount userAcc = ((KcpUserAccount)uk);                    
                }
                else if (keyType == typeof( KcpKeyFile))
                {
                    XmlElement keyFileEl = compositeKeyXML.CreateElement("KeyFile");
                    compositeKeyEl.AppendChild(keyFileEl);
                    keyFileEl.InnerText = ((KcpKeyFile)uk).Path;
                }
            }
            compositeKeyXML.Save(compKeyFile);
        }

        public static CompositeKey Load(string fileLocation)
        {
            CompositeKey key = new CompositeKey();
            using (XmlTextReader xmlreader = new XmlTextReader(fileLocation))
            {
                xmlreader.WhitespaceHandling = WhitespaceHandling.None;
                xmlreader.MoveToContent();
                XmlNode root = new XmlDocument().ReadNode(xmlreader);
                XmlNodeList nodelist = root.ChildNodes;
                foreach (XmlNode node in nodelist)
                {
                    switch (node.Name)
                    {

                        case "Password":
                            key.AddUserKey(new KcpPassword(node.InnerText));
                            break;
                        case "UserAccount":
                            key.AddUserKey(new KcpUserAccount());
                            break;
                        case "KeyFile":
                            key.AddUserKey(new KcpKeyFile(node.InnerText));
                            break;
                    }
                    
                }
            }

            return key;
        }

    }
}
